package com.lanjiyin.cloud.common.util;

import java.net.URLEncoder;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;
import java.util.Map.Entry;
import java.util.Set;
import java.util.TreeSet;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

import org.apache.commons.lang3.StringUtils;

import com.alibaba.fastjson.JSONObject;
import com.lanjiyin.cloud.common.util.sensi.SensitiveFilter;

import lombok.extern.slf4j.Slf4j;

@Slf4j
public class StringHelper {
	
	public static String getObjectValue(Object obj) {
		return obj == null ? "" : obj.toString();
	}

	/**
	 * 判断字符串是否包含汉字
	 * 
	 * @param txt
	 * @return
	 */
	public static Boolean containsCN(String txt) {
		if (StringUtils.isBlank(txt)) {
			return false;
		}
		for (int i = 0; i < txt.length(); i++) {

			String bb = txt.substring(i, i + 1);

			boolean cc = Pattern.matches("[\u4E00-\u9FA5]", bb);
			if (cc)
				return cc;
		}
		return false;
	}

	/**
	 * 去掉HTML代码
	 * 
	 * @param news
	 * @return
	 */
	public static String removeHtml(String news) {
		String s = news.replaceAll("amp;", "").replaceAll("<", "<").replaceAll(">", ">");

		Pattern pattern = Pattern.compile("<(span)?\\sstyle.*?style>|(span)?\\sstyle=.*?>", Pattern.DOTALL);
		Matcher matcher = pattern.matcher(s);
		String str = matcher.replaceAll("");

		Pattern pattern2 = Pattern.compile("(<[^>]+>)", Pattern.DOTALL);
		Matcher matcher2 = pattern2.matcher(str);
		String strhttp = matcher2.replaceAll(" ");

		String regEx = "(((http|https|ftp)(\\s)*((\\:)|：))(\\s)*(//|//)(\\s)*)?"
				+ "([\\sa-zA-Z0-9(\\.|．)(\\s)*\\-]+((\\:)|(:)[\\sa-zA-Z0-9(\\.|．)&%\\$\\-]+)*@(\\s)*)?" + "("
				+ "(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9])"
				+ "(\\.|．)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)"
				+ "(\\.|．)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[1-9]|0)"
				+ "(\\.|．)(25[0-5]|2[0-4][0-9]|[0-1]{1}[0-9]{2}|[1-9]{1}[0-9]{1}|[0-9])"
				+ "|([\\sa-zA-Z0-9\\-]+(\\.|．)(\\s)*)*[\\sa-zA-Z0-9\\-]+(\\.|．)(\\s)*[\\sa-zA-Z]*" + ")"
				+ "((\\s)*(\\:)|(：)(\\s)*[0-9]+)?" + "(/(\\s)*[^/][\\sa-zA-Z0-9\\.\\,\\?\\'\\\\/\\+&%\\$\\=~_\\-@]*)*";
		Pattern p1 = Pattern.compile(regEx, Pattern.DOTALL);
		Matcher matchhttp = p1.matcher(strhttp);
		String strnew = matchhttp.replaceAll("").replaceAll("(if[\\s]*\\(|else|elseif[\\s]*\\().*?;", " ");

		Pattern patterncomma = Pattern.compile("(&[^;]+;)", Pattern.DOTALL);
		Matcher matchercomma = patterncomma.matcher(strnew);
		String strout = matchercomma.replaceAll(" ");
		String answer = strout.replaceAll("[\\pP‘’“”]", " ").replaceAll("\r", " ").replaceAll("\n", " ")
				.replaceAll("\\s", " ").replaceAll("　", "");

		return answer;
	}

	/**
	 * 把数组的空数据去掉
	 * 
	 * @param array
	 * @return
	 */
	public static List<String> array2Empty(String[] array) {
		List<String> list = new ArrayList<String>();
		for (String string : array) {
			if (StringUtils.isNotBlank(string)) {
				list.add(string);
			}
		}
		return list;
	}

	/**
	 * 把数组转换成set
	 * 
	 * @param array
	 * @return
	 */
	public static Set<?> array2Set(Object[] array) {
		Set<Object> set = new TreeSet<Object>();
		for (Object id : array) {
			if (null != id) {
				set.add(id);
			}
		}
		return set;
	}

	/**
	 * URL参数Encoded编码
	 * 
	 * @param paramString
	 * @return
	 */
	public static String toURLEncoded(String paramString) {
		if (paramString == null || paramString.equals("")) {
			return "";
		}

		try {
			String str = new String(paramString.getBytes(), "UTF-8");
			str = URLEncoder.encode(str, "UTF-8");
			return str;
		} catch (Exception localException) {

		}

		return "";
	}

	/**
	 * 把一串参数字符串,转换成Map 如"?a=3&b=4" 转换为Map{a=3,b=4}
	 * 
	 * @param args
	 * @return
	 */
	public static Map<String, ? extends Object> convertUrlParamsToMap(String args) {
		if (StringUtils.isBlank(args)) {
			return null;
		}
		args = args.trim();
		// 如果是?开头,把?去掉
		if (args.startsWith("?")) {
			args = args.substring(1, args.length());
		}
		String[] argsArray = args.split("&");

		Map<String, Object> result = new HashMap<String, Object>();
		for (String ag : argsArray) {
			if (!StringUtils.isBlank(ag) && ag.indexOf("=") > 0) {

				String[] keyValue = ag.split("=");
				// 如果value或者key值里包含 "="号,以第一个"="号为主 ,如 name=0=3 转换后,{"name":"0=3"},
				// 如果不满足需求,请勿修改,自行解决.

				String key = keyValue[0];
				String value = "";
				for (int i = 1; i < keyValue.length; i++) {
					value += keyValue[i] + "=";
				}
				value = value.length() > 0 ? value.substring(0, value.length() - 1) : value;
				result.put(key, value);

			}
		}

		return result;
	}

	/**
	 * JSON对象转Map
	 * 
	 * @param jsonObj
	 * @return Map<String,Object>
	 */
	public static Map<String, Object> convertJsonToMap(JSONObject jsonObj) {
		Map<String, Object> paramMap = new HashMap<>();
		Iterator it = jsonObj.entrySet().iterator();
		while (it.hasNext()) {
			Map.Entry<String, Object> entry = (Entry<String, Object>) it.next();
			paramMap.put(entry.getKey(), entry.getValue());
		}
		return paramMap;
	}

	/**
	 * 过滤敏感词
	 * 
	 * @param content
	 * @return String
	 */
	public static String StringSensiFilter(String content) {
		if (StringUtils.isBlank(content)) {
			return content;
		} else {
			try {
				// 过滤手机号
				// 存在国际电话。规则：7位以上的连续字符即认定为电话号码
				String mobilePattern = "(([0-9]|([0-9][.-\\|\\\\=@#%^&*])){5})([0-9]+)";
				content = content.replaceAll(mobilePattern, "***");
				String mobilePattern1 = "(([一]|[1])(([三四五七八]|[0-9]))([一二三四五六七八九零]|[0-9]))(([一二三四五六七八九零]|[0-9])+)";
				content = content.replaceAll(mobilePattern1, "***");
				// 过滤邮件地址
				content = content.replaceAll("(\\w?)(\\w+)(\\w)(@\\w+\\.[a-z]+(\\.[a-z]+)?)", "***");
				// 过滤微信
				content = content.replaceAll("([WwVv]|[微])(\\w+|[ ]+)([xX]|[信])", "***");
				content = content.replaceAll("([a-zA-Z_0-9]{6})([a-zA-Z_0-9]+)", "***");
				// 过滤敏感词
//				// 进行过滤
				content = new SensitiveFilter().getInstance().filter(content, '*');
			} catch (Exception se) {
				log.error("过滤敏感词异常", se);
			}
			return content;
		}
	}
}
